import request from '@/utils/request/request';
import requestForFile from '@/utils/request/requestForFile';
import {
  CodegenListRes,
  CodegenParams,
  CodegenConfigRecord,
  CodegenRecordReq,
  DbTableReq,
  TableRecordReq,
} from './model/codegenModel';

// 分页查询代码生成器列表
export function queryCodegenRecordPage(params: CodegenParams) {
  return request.get<CodegenListRes>('/api/infra/codegen/table/page', {
    params,
  });
}

// 导入数据库表以创建代码生成器
export function createCodegenRecord(data: CodegenRecordReq) {
  return request.post('/api/infra/codegen/create-list', {
    ...data,
  });
}

// 更新代码生成器配置
export function updateCodegenRecord(data: CodegenConfigRecord) {
  return request.put('/api/infra/codegen/update', {
    ...data,
  });
}

// 删除代码生成器
export function removeCodegenRecord(tableId: string) {
  return request.delete('/api/infra/codegen/delete', {
    params: { tableId },
  });
}

// 同步代码（更新）
export function syncCodegenRecord(tableId: string) {
  return request.put('/api/infra/codegen/sync-from-db', {
    params: { tableId },
  });
}

// 预览生成代码
export function previewGeneratedCode(tableId: string) {
  return request.get('/api/infra/codegen/preview', { params: { tableId } });
}

// 下载生成代码
export function downloadGeneratedCode(tableId: string) {
  return requestForFile.get('/api/infra/codegen/download', {
    params: { tableId },
    responseType: 'blob',
  });
}

// 获取生成器配置详细信息
export function detailCodegenRecord(tableId: string) {
  return request.get('/api/infra/codegen/detail', {
    params: { tableId },
  });
}

// 获取数据库表
export function queryDbTableRecord(params: DbTableReq) {
  return request.get('/api/infra/codegen/db/table/list', { params });
}

// 创建数据表
export function createTableRecord(data: TableRecordReq) {
  return request.post('/api/infra/codegen/db/table', data);
}
